package com.junci.mtest.views.fragment;

import android.annotation.SuppressLint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.tabs.TabLayout;
import com.junci.mtest.R;


import com.junci.mtest.databinding.FragmentKeBinding;
import com.junci.mtest.views.BaseFragment;
import com.junci.mtest.vm.MainViewModle;

import java.util.ArrayList;

/**
 * Created by YM
 * Create Date 2024/5/1 0:46
 * Used 首页课程
 */
public class MainLessonFragment extends BaseFragment {
    private static MainLessonFragment f;
    private View view;
    public static MainLessonFragment newInstance() {
        if (f == null) {
            f = new MainLessonFragment();
        }
        return f;
    }
    private ArrayList<Fragment> fragments ;
    private ArrayList<TabLayout.Tab> Tabs ;
    private ViewPager2 viewPager;
    FragmentKeBinding binding;


    MainViewModle viewModle;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            binding =  DataBindingUtil.inflate(inflater, R.layout.fragment_ke, container, false);
            viewModle = new ViewModelProvider(this).get(MainViewModle.class);
            binding.setViewModel(viewModle);
            view = binding.getRoot();
        }
        bindOb();
        initToolbar();
        initPagers();
        return view;
    }

    @SuppressLint("FragmentLiveDataObserve")
    private void bindOb() {
        Observer<String> titleOb = new Observer<String>() {
            @Override
            public void onChanged(String s) {
                binding.collapsingToolbarLayout.setTitle(s);
            }
        };
        viewModle.getTitle().observe(this,titleOb);
    }

    private void initToolbar(){
        binding.appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (verticalOffset == 0) {
                    // Toolbar完全展开
                    binding.tabMode.setVisibility(View.VISIBLE);
                    binding.collapsingToolbarLayout.setTitleEnabled(false);
                } else {
                    // Toolbar折叠时
                    binding.tabMode.setVisibility(View.GONE);
                    binding.collapsingToolbarLayout.setTitleEnabled(true);
                }
            }
        });
    }
    private void initPagers(){
        /*
           二级页面
           训练课程 [LessonFragment]
           训练计划 [PlanFragment]
           动作库 [ActionFragment]
         */
        fragments = new ArrayList<>();
        fragments.add(LessonFragment.newInstance());
        fragments.add(new PlanFragment());
        fragments.add(ActionFragment.newInstance());
        // 设置适配器
        binding.viewpager.setAdapter(new PagerAdapter(this));
        Tabs=new ArrayList<>();
        Tabs.add(binding.tabMode.newTab().setText("训练课程").setCustomView(R.layout.tab_main_lesson));
        Tabs.add(binding.tabMode.newTab().setText("训练计划").setCustomView(R.layout.tab_main_lesson));
        Tabs.add(binding.tabMode.newTab().setText("动作库").setCustomView(R.layout.tab_main_lesson));
        for (int i = 0; i < Tabs.size(); i++) {
            binding.tabMode.addTab(Tabs.get(i));
            View view = binding.tabMode.getTabAt(i).getCustomView(); // 获取Tab的View
            if (i==0) {
                binding.tabMode.getTabAt(i).select();
            }
            if (view != null) {
                TextView title = view.findViewById(R.id.title);
                View line = view.findViewById(R.id.line);
                title.setText(binding.tabMode.getTabAt(i).getText());
                if(binding.tabMode.getTabAt(i).isSelected()){
                    line.setVisibility(View.VISIBLE);
                    title.setTextScaleX(1.2f);
                    title.setScaleY(1.2f);
                    title.setTypeface(null, Typeface.BOLD);
                }else{
                    line.setVisibility(View.GONE);
                    title.setTextScaleX(1.0f);
                    title.setScaleY(1.0f);
                    title.setTypeface(null, Typeface.NORMAL);
                }
            }
        }

        binding.tabMode.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView(); // 获取Tab的View
                binding.viewpager.setCurrentItem(tab.getPosition());
                if (view != null) {
                    TextView title = view.findViewById(R.id.title);
                    View line = view.findViewById(R.id.line);
                    title.setTextScaleX(1.2f);
                    title.setScaleY(1.2f);
                    title.setTypeface(null, Typeface.BOLD);
                    line.setVisibility(View.VISIBLE);
                }
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView(); // 获取Tab的View
                if (view != null) {
                    TextView title = view.findViewById(R.id.title);
                    View line = view.findViewById(R.id.line);
                    title.setTextScaleX(1.0f);
                    title.setScaleY(1.0f);
                    title.setTypeface(null, Typeface.NORMAL);
                    line.setVisibility(View.GONE);

                }
            }
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

        binding.viewpager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                binding.tabMode.selectTab(Tabs.get(position));
                viewModle.setTitle(Tabs.get(position).getText().toString());
            }
        });


    }

    /**
     * 二级页面
     * 训练课程 [LessonFragment]
     *
     */
    class PagerAdapter extends FragmentStateAdapter{

        public PagerAdapter(@NonNull Fragment fragment) {
            super(fragment);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return fragments.get(position);
        }

        @Override
        public int getItemCount() {
            return fragments.size();
        }
    }


}
